package com.zk.babala.presenter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.leanback.widget.Presenter;

import com.bumptech.glide.Glide;
import com.zk.babala.R;
import com.zk.babala.activity.DetailsActivity;
import com.zk.babala.entity.Movie;

/**
 * @Author: zhangke
 * @Date: 2025/8/7 15:56
 */
public class MovieItemPresenter extends Presenter {
    @Override
    public Presenter.ViewHolder onCreateViewHolder(ViewGroup viewGroup) {
        View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.search_item, viewGroup, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, Object item) {
        Movie movie = (Movie) item;
        TextView textView = viewHolder.view.findViewById(R.id.search_item_text);
        textView.setText(movie.getTitle());
        ImageView imageView = viewHolder.view.findViewById(R.id.search_item_image);
        Glide.with(viewHolder.view.getContext()).load(movie.getCardImageUrl())
                .placeholder(R.drawable.default_movie_image) // 加载中显示默认图片
                .error(R.drawable.default_movie_image)
                .into(imageView);

        if (movie.getVideoUrl() == null) {
            return;
        }
        LinearLayout linearLayout = viewHolder.view.findViewById(R.id.search_item);
        linearLayout.setOnClickListener(v -> {
            Intent intent = new Intent(viewHolder.view.getContext(), DetailsActivity.class);
            intent.putExtra(DetailsActivity.MOVIE, movie);
            viewHolder.view.getContext().startActivity(intent);
        });
    }

    @Override
    public void onUnbindViewHolder(ViewHolder viewHolder) {
    }

}
